import createHistory from "history/createBrowserHistory";
import Cookies from "js-cookie";
import Helper from "./helper";
const history = createHistory({
  forceRefresh: true, // 从服务器重新拉取数据
});

class Common extends Helper {
  constructor(name) {
    super();
    this.name = name;
    this.init();
  }

  init() {
    this.sundryFunction();
    this.showMore();
    this.goBack();
    this.userCenter();
    this.allPage();
  }

  sundryFunction() {
    const $ = this.$, article_des = $('.article_des')
    $(".js_close").on("click", function() {
      $(this)
        .parent()
        .hide();
    });

    $(".js_toSearch").on("focus", function() {
      window.location.href = "//m.veryeast.cn/search";
    });

    $(".js_matching").on("click", function() {
      const jobID = $('.js_cllect').data('job_id')
      window.location.href = `/service/matching/detail?job_id=${jobID}`
    });

    $(".js_competitive").on("click", function() {
      const jobID = $('.js_cllect').data('job_id')
      window.location.href = `/service/competitive/detail?job_id=${jobID}`
    });

    $(".js_go_register").on("click", function() {
      window.zhuge.track('首页浮窗立即注册')
      window.location.href =
        "//m.veryeast.cn/user/register?sss=" + window.location.href + "&redirect=" + window.location.pathname;
    });
    $.each(article_des, function() {
      let html = $(this).html().trim()
      if(html.length > 61) {
        $(this).append('<span>...</span>')
      }
    })
  }

  userCenter() {
    const $ = this.$,
      me = $(".js_userCenter");
    me.on("click", () => {
      const ticket = Cookies.get("ticket");
      if (ticket) {
        window.location.href = "//m.veryeast.cn/tabs/user";
      } else {
        window.location.href = "//m.veryeast.cn/user/login";
      }
    });
  }

  allPage() {
    const $ = this.$,
      me = $(".js_all_page"),
      body = $("#city_page");
    me.on("click", e => {
      e.stopPropagation();
      me.siblings(".js_all").slideToggle("fast");
    });
    body.on("click", () => {
      $(".js_all").slideUp("fast");
    });
  }

  goBack() {
    const $ = this.$;
    $(".js_back").on("click", () => {
      (window.history.length === 1) ? (window.location.href = "//m.veryeast.cn") : history.go(-1);
    });
  }

  showMore() {
    const $ = this.$,
      me = $(".js_more");
    me.on("click", () => {
      if (me.hasClass("route")) {
        me.siblings(".js_des").removeClass("more");
        me.removeClass("route");
      } else {
        me.siblings(".js_des").addClass("more");
        me.addClass("route");
      }
    });
  }
}

export default Common;
